#!/bin/bash

# Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


set -e

pushd ${1}
echo "obcv45dd: [$1], [$2], [$3], [$4], [$5], [$6], [$7]"
ROOT_BUILD_DIR=${2}
ROOT_DIR=${2}/../..
MODULES_SOURCE=${3}
export KERNELBUILD_PATH=${4}
PRODUCT_COMPANY=${5}
PRODUCT_NAME=${6}
KERNEL_VERSION=${7}

export PATH=${ROOT_DIR}/prebuilts/clang/ohos/linux-x86_64/llvm/bin/:$PATH
echo "PATH = [$PATH]"
export KERNELPATH=${KERNELBUILD_PATH}/OBJ/${KERNEL_VERSION}
echo "KERNELPATH = [$KERNELPATH]"
export KERNELARCH=arm64
export TOOLPREFIX_PATH=${ROOT_DIR}/prebuilts/gcc/linux-x86/aarch64/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/
export TOOLPREFIX=${TOOLPREFIX_PATH}aarch64-linux-gnu-
export WSCFG_KERNEL_DIR=${KERNELPATH}
export WSCFG_ARCH_NAME=${KERNELARCH}
export KSRC=${KERNELPATH}
export WSCFG_CROSS_COMPILE=${TOOLPREFIX}
echo "TOOLPREFIX = [$TOOLPREFIX]"
MAKE="make LLVM=1 LLVM_IAS=1 -j16"
SIGN_FILE="${KERNELPATH}/scripts/sign-file"
SIGN_METHOD="sha512"
CERT_FILE="${KERNELPATH}/certs/signing_key.pem ${KERNELPATH}/certs/signing_key.x509"


export DIR_MAP_CONFIG_FILE=release.mk

export BSP_MODULES_OUT=${ROOT_BUILD_DIR}/modules

INCLUDE_NET_PATH=${KERNELBUILD_PATH}/src_tmp/${KERNEL_VERSION}/include/net/

rm -rf ${BSP_MODULES_OUT}

mkdir -p ${BSP_MODULES_OUT}

############  build sle  ##################
cp -rf ${MODULES_SOURCE}/sle ${BSP_MODULES_OUT}/sle

${MAKE} -C ${BSP_MODULES_OUT}/sle -f Makefile  O=${BSP_KERNEL_OUT} \
	BSP_MODULES_OUT=${BSP_MODULES_OUT} platform

${MAKE} -C ${BSP_MODULES_OUT}/sle -f Makefile  O=${BSP_KERNEL_OUT} \
	BSP_MODULES_OUT=${BSP_MODULES_OUT} sle

${MAKE} -C ${BSP_MODULES_OUT}/sle -f Makefile  O=${BSP_KERNEL_OUT} \
	BSP_MODULES_OUT=${BSP_MODULES_OUT} wifi

${MAKE} -C ${BSP_MODULES_OUT}/sle -f Makefile  O=${BSP_KERNEL_OUT} \
	BSP_MODULES_OUT=${BSP_MODULES_OUT} ble_android

${MAKE} -C ${BSP_MODULES_OUT}/sle -f Makefile  O=${BSP_KERNEL_OUT} \
	BSP_MODULES_OUT=${BSP_MODULES_OUT} dft_driver

[ -f ${SIGN_FILE} ] && {
    ${SIGN_FILE} ${SIGN_METHOD} ${CERT_FILE} ${BSP_MODULES_OUT}/sle/output/bin/plat_soc.ko
    ${SIGN_FILE} ${SIGN_METHOD} ${CERT_FILE} ${BSP_MODULES_OUT}/sle/output/bin/sle_soc.ko
    ${SIGN_FILE} ${SIGN_METHOD} ${CERT_FILE} ${BSP_MODULES_OUT}/sle/output/bin/wifi_soc.ko
    ${SIGN_FILE} ${SIGN_METHOD} ${CERT_FILE} ${BSP_MODULES_OUT}/sle/output/bin/ble_soc.ko
    ${SIGN_FILE} ${SIGN_METHOD} ${CERT_FILE} ${BSP_MODULES_OUT}/sle/output/bin/bp_test.ko
}

sleep 3
touch ${MODULES_SOURCE}/build_modules.sh
popd

